package com.example.bookkeepingelf.business.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.bookkeepingelf.R;
import com.example.bookkeepingelf.base.utils.ResourceUtils;
import com.example.bookkeepingelf.business.database.entity.AccountBook;
import com.example.bookkeepingelf.business.listener.MyOnClickListener;

import java.util.List;

public class AccountBookHomeListViewAdapter extends ArrayAdapter<AccountBook> {

    private LayoutInflater layoutInflater;
    private int resourceId;
    private MyOnClickListener<AccountBook> myOnClickListener;


    public AccountBookHomeListViewAdapter(@NonNull Context context, int resource,
                                          List<AccountBook> items,
                                          MyOnClickListener<AccountBook> myOnClickListener) {
        super(context, resource, items);
        resourceId = resource;

        this.myOnClickListener = myOnClickListener;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        // 获取Logo对象
        AccountBook accountBook = this.getItem(position);
        // 加载布局
        View view = layoutInflater.inflate(resourceId, parent, false);
        // 设置图片和文字
        ImageView imageView = view.findViewById(R.id.account_book_image);
        TextView accountBookNameView = view.findViewById(R.id.account_book_name);
        int iconResourceId = ResourceUtils.getIconResourceId(getContext(), accountBook.getIcon());
        imageView.setImageResource(iconResourceId);
        accountBookNameView.setText(accountBook.getAccountBookName());

        view.setOnClickListener(view1 -> {
            if (myOnClickListener != null){
                this.myOnClickListener.onMyClick(accountBook);
            }
        });
        return view;
    }

}
